package com.example.JavaWebApi.core.mybatisplusextention;


import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.util.Collections;

/**
 * @program: java-web-api
 * @description: 自动生成实体类
 * @author: yang ze
 * @create: 2021-11-30 09:25
 */
@Component
@Data
public class EntityAutoGenerator {
    @SneakyThrows
    /***
     * 当需要生成代码时，将此类作为主类运行，现在的配置只生成entity，mapper接口，service实现文件
     * 生成之后可能有的实体名称和别的文件引用的文件包路径有些不一样 手动改一下即可
     */
    public static void main(String[] args){
        init();
    }

    private static String ConnectionString
            = "jdbc:mysql://182.92.176.113:6033/lvzhengcao_base?useUnicode=true&useSSL=false&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=GMT%2B8";
    private static String UserName = "admin";
    private static String PassWord="adks@123";
    private static String projectName="JavaWebApi";
    public static void init()  {
        FastAutoGenerator.create(ConnectionString,UserName,PassWord)
                .globalConfig(builder -> {
                    builder.author("杨泽")
                            .enableSwagger()
                            .outputDir(System.getProperty("user.dir")+"/src/main/java")
                            .disableOpenDir()
                            .build();
                })
                .packageConfig(builder -> {
                    builder.moduleName(projectName)
                            .parent("com.example")
                            .service("service")
                            .mapper("mapper")
                            .entity("model.entity")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, System.getProperty("user.dir") + "/src/main/resources/mapper/"))
                            .build();
                })
                .templateConfig(builder -> {
                    builder.entity("templates/entity.java.vm")
                            .mapper("templates/mapper.java.vm")
                            .service("templates/service.java.vm")
                            .serviceImpl("templates/serviceImpl.java.vm")
                            //.xml("templates/mapper.xml.vm")
                            .build();
                })
                .strategyConfig(builder -> {
                    builder
                            .addExclude("sys_orginfo", "sys_roleinfo", "sys_rolemapmenu", "sys_userinfo", "sys_usermaporg", "sys_usermaprole", "sys_menuinfo", "sys_dictionary")
                            .addTablePrefix("sys_")
                            .entityBuilder()
                            .enableLombok()
                            .addTableFills(new Column("CreateTime", FieldFill.INSERT))
                            .addTableFills(new Column("UpdateTime", FieldFill.INSERT_UPDATE))
                            .idType(IdType.AUTO)
                            .controllerBuilder()
                            .enableRestStyle()
                            .build()
                            .serviceBuilder()
                            .formatServiceFileName("I%sService")
                            .mapperBuilder()
                            .enableBaseResultMap()
                            .build();
                })
                .execute();
    }
}